//
//  HCAlbumDetailController.m
//  HappyChildren
//
//  Created by jiayibai-02por on 2017/5/31.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCAlbumDetailController.h"

@interface HCAlbumDetailController ()<UIScrollViewDelegate>
{
    BOOL ishidden_;
    CGRect minFrame;
    CGRect maxFrame;
    UIImageView *currentImageView;
    UILabel *indexLabel;
}
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *bottomView;
@property (nonatomic, strong) UIView *topView;


@end

@implementation HCAlbumDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.imageArray = [NSArray arrayWithArray:self.parameterObject];
    NSString *time = [self.dataObject objectForKey:@"time"];
    NSString *contents = [self.dataObject objectForKey:@"content"];
    self.textTitle = time;
    
    indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 64-14, 100, 12)];
    indexLabel.textColor = [UIColor blackColor];
    indexLabel.font = [UIFont systemFontOfSize:10];
    indexLabel.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2.0+45, indexLabel.center.y);
    [self.headerView addSubview:indexLabel];
    indexLabel.text = [NSString stringWithFormat:@"1/%ld",self.imageArray.count];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, self.centerView__.frame.size.height)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.contentSize = CGSizeMake(Screen_width *self.imageArray.count, self.centerView__.frame.size.height);
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    [self.centerView__ addSubview:self.scrollView];
    
    for (int i = 0; i < self.imageArray.count; i++) {
        
        UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(Screen_width*i, 0, Screen_width, self.scrollView.frame.size.height)];
        NSURL *imageUrl = [NSURL URLWithString:self.imageArray[i]];
        [imageView sd_setImageWithURL:imageUrl placeholderImage:nil];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.tag = 100 + i;
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClick)];
        [imageView addGestureRecognizer:tap];
        
        [self addGestureToView:imageView];
//        [self addPanGestureToView:imageView];
         [self.scrollView addSubview:imageView];
    }
    
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.centerView__.frame.size.height-100, Screen_width, 100)];
    self.bottomView.backgroundColor = [UIColor blackColor];
    [self.centerView__ addSubview:self.bottomView];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, Screen_width - 20, 100)];
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.textColor = [UIColor whiteColor];
    contentLabel.numberOfLines = 0;
    contentLabel.text = contents;
    CGSize maxSize = CGSizeMake(Screen_width-20, 100-49-5);
    CGSize labelSize = [self labAutoCalculateRectWith:contentLabel.text FontSize:14 MaxSize:maxSize];
    [contentLabel setFrame:CGRectMake(10, 100-labelSize.height-49-5, Screen_width-20, labelSize.height)];
    [self.bottomView addSubview:contentLabel];
    ishidden_ = NO;
    
    UIImageView *imageView = [self.scrollView viewWithTag:100];
    currentImageView = imageView;
    minFrame = imageView.frame;
    maxFrame = CGRectMake(imageView.frame.origin.x-300, imageView.frame.origin.y-300, imageView.frame.size.width+600, imageView.frame.size.height + 600);
}

#pragma mark -- 缩放手势
- (void)addGestureToView:(UIImageView *)view {

    UIPinchGestureRecognizer *gesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [view addGestureRecognizer:gesture];
}

#pragma mark -- 移动手势
- (void)addPanGestureToView:(UIImageView *)view {

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [view addGestureRecognizer:gesture];
}

- (void)pinchView:(UIPinchGestureRecognizer *)gesture {

    UIView *view = gesture.view;
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged)
    {
        view.transform = CGAffineTransformScale(view.transform, gesture.scale, gesture.scale);
        if (view.frame.size.width > maxFrame.size.width) {
            
            [UIView animateWithDuration:0.2 animations:^{
               
                view.frame = maxFrame;
            }];
        }
        if (view.frame.size.width < minFrame.size.width) {
            
            [UIView animateWithDuration:0.2 animations:^{
                
                view.frame = minFrame;
            }];
        }
        gesture.scale = 1;
    }
}

- (void)panGesture:(UIPanGestureRecognizer *)gesture {

    UIView *imageView = gesture.view;
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gesture translationInView:imageView.superview];
        [imageView setCenter:(CGPoint){imageView.center.x + translation.x, imageView.center.y + translation.y}];
        [gesture setTranslation:CGPointZero inView:imageView.superview];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    NSInteger index = scrollView.contentOffset.x/Screen_width;
    UIImageView *imageView = [scrollView viewWithTag:100 + index];
    currentImageView = imageView;
    minFrame = imageView.frame;
    maxFrame = CGRectMake(imageView.frame.origin.x-300, imageView.frame.origin.y-300, imageView.frame.size.width+600, imageView.frame.size.height + 600);
    indexLabel.text = [NSString stringWithFormat:@"%ld/%ld",index+1,self.imageArray.count];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    [UIView animateWithDuration:0.2 animations:^{
       
        currentImageView.frame = minFrame;
    }];
}

#pragma mark --图片点击事件
- (void)imageViewClick {

    if (!ishidden_) {
        
        [UIView animateWithDuration:0.3 animations:^{
            
            [self.bottomView setFrame:CGRectMake(0, self.centerView__.frame.size.height, Screen_width, 100)];
            [self.headerView setFrame:CGRectMake(0, -64, Screen_width, 64)];
        }];
        ishidden_ = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"statusBar" object:nil userInfo:@{@"state":@"1"}];
    }
    else {
        
        [UIView animateWithDuration:0.3 animations:^{
            
            [self.bottomView setFrame:CGRectMake(0, self.centerView__.frame.size.height-100, Screen_width, 100)];
            [self.headerView setFrame:CGRectMake(0, 0, Screen_width, 64)];
        }];
        ishidden_ = NO;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"statusBar" object:nil userInfo:@{@"state":@"0"}];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleLightContent;
}

- (CGSize)labAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
{
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
    NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize;
    
    //如果是IOS6.0
    if (![text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
        labelSize = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
    }
    //如果系统为iOS7.0
    else{
        // iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
        labelSize = [text boundingRectWithSize: maxSize
                                       options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine
                                    attributes:attributes
                                       context:nil].size;
    }
    labelSize.height=ceil(labelSize.height);
    labelSize.width=ceil(labelSize.width);
    return labelSize;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
